package com.atmilan.quartz.sync.pull.buyer.converter;

import java.util.List;

import org.mapstruct.AfterMapping;
import org.mapstruct.BeanMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;
import org.springframework.beans.factory.annotation.Autowired;

import com.atmilan.quartz.model.dto.buyer.BuyerProductResDTO;
import com.atmilan.quartz.model.entity.buyer.BuyerProduct;
import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.extern.slf4j.Slf4j;

/**
 * @author hupeng
 */
@Slf4j
@Mapper(componentModel = "spring")
public abstract class BaseBuyerProductConverter {

    @Autowired
    private ObjectMapper objectMapper;

    @BeanMapping(qualifiedByName = "toModel")
    public abstract BuyerProduct toModel(BuyerProductResDTO.Product vo);

    @AfterMapping
    @Named("toModel")
    protected void toModelAfterMapping(BuyerProductResDTO.Product vo, @MappingTarget BuyerProduct model) {
        try {
            model.setGallery(objectMapper.writeValueAsString(vo.getGalleryDTO()));
        } catch (Exception e) {
            log.error("parse gallery error ", e);
        }
    }

    public abstract List<BuyerProduct> toModelList(List<BuyerProductResDTO.Product> voList);

}
